﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class AlternateSku : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlDefaultUom;
        protected DropDownList ddlType;
        protected HtmlForm F2063;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected Label lblAlternateSku;
        protected Label lblAlternteSku;
        protected Label lblDefaultUom;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblPack;
        protected Label lblSkuID;
        protected Label lblType;
        protected Label lblUdf1;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf4;
        protected Label lblUdf5;
        protected Label lblVendor;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAlternteSku;
        protected TextBox txtOwnerID;
        protected TextBox txtPack;
        protected TextBox txtSkuID;
        protected TextBox txtUdf1;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtVendor;

        private void BindData2UomList(string packID)
        {
            try
            {
                base.BindUomList2DDL(this.ddlDefaultUom, packID);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2210_002") + ex.Message;
                this.txtPack.Focus();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Sku.aspx?from={0}&currTab={1}&ID={2}&ID2={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "3", base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()) }), true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtAlternteSku.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2210_003");
            }
            else if (string.IsNullOrEmpty(this.txtPack.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2210_004");
            }
            else
            {
                bool success = false;
                try
                {
                    AlternateSkuInfo alternateSkuInfo = new AlternateSkuInfo {
                        OwnerID = this.txtOwnerID.Text.Trim(),
                        SkuID = this.txtSkuID.Text.Trim(),
                        AltSkuID = this.txtAlternteSku.Text.Trim(),
                        Type = this.ddlType.SelectedValue,
                        VendorID = this.txtVendor.Text.Trim(),
                        PackID = this.txtPack.Text.Trim(),
                        DefaultUomID = this.ddlDefaultUom.SelectedValue,
                        Udf1 = this.txtUdf1.Text.Trim(),
                        Udf2 = this.txtUdf2.Text.Trim(),
                        Udf3 = this.txtUdf3.Text.Trim(),
                        Udf4 = this.txtUdf4.Text.Trim(),
                        Udf5 = this.txtUdf5.Text.Trim(),
                        IsActive = this.chkIsActive.Checked ? "Y" : "N",
                        CreatedBy = "DBA"
                    };
                    if (!string.IsNullOrEmpty(this.hdnType.Value) && (this.hdnType.Value.ToUpper() == "EDIT"))
                    {
                        alternateSkuInfo.UpdatedBy = base.UserInfo.UserID;
                        alternateSkuInfo.UpdatedDate = DateTime.Now;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(alternateSkuInfo, "ALTERNATE_SKU")))
                        {
                            success = true;
                        }
                    }
                    else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(alternateSkuInfo, "ALTERNATE_SKU")))
                    {
                        success = true;
                    }
                    if (success)
                    {
                        base.Response.Redirect(string.Format("SKU.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtSkuID.Text.Trim())), false);
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2210_001") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("Sku.aspx?from={0}&currTab={1}&ID={2}&ID2={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "3", base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()) }));
                }
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlType, "AlternateSkuType");
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID1"].ToString()))
            {
                string ownerID = base.Request.QueryString["ID1"].ToString();
                this.txtOwnerID.Text = ownerID;
                this.txtOwnerID.Enabled = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID2"].ToString()))
            {
                string skuID = base.Request.QueryString["ID2"].ToString();
                this.txtSkuID.Text = skuID;
                this.txtSkuID.Enabled = false;
            }
            if (base.Request.QueryString["AltSkuID"] != null)
            {
                string AltSkuID = base.Request.QueryString["AltSkuID"].ToString();
                this.txtAlternteSku.Text = AltSkuID;
                this.txtAlternteSku.Enabled = false;
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetAlternateSku2Form();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetAlternateSku2Form()
        {
            try
            {
                AlternateSkuInfo alternateSku = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtOwnerID.Text.Trim(), this.txtSkuID.Text.Trim(), this.txtAlternteSku.Text.Trim(), "ALTERNATE_SKU") as AlternateSkuInfo;
                if (alternateSku != null)
                {
                    this.ddlType.SelectedValue = alternateSku.Type;
                    this.txtVendor.Text = alternateSku.VendorID;
                    this.txtPack.Text = alternateSku.PackID;
                    if (!string.IsNullOrEmpty(this.txtPack.Text.Trim()))
                    {
                        this.BindData2UomList(this.txtPack.Text.Trim());
                        this.ddlDefaultUom.SelectedValue = alternateSku.DefaultUomID;
                    }
                    this.txtUdf1.Text = alternateSku.Udf1;
                    this.txtUdf2.Text = alternateSku.Udf2;
                    this.txtUdf3.Text = alternateSku.Udf3;
                    this.txtUdf4.Text = alternateSku.Udf4;
                    this.txtUdf5.Text = alternateSku.Udf5;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void txtPack_TextChanged(object sender, EventArgs e)
        {
            this.BindData2UomList(this.txtPack.Text.Trim());
        }
    }
}

